package com.example.milimili;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.milimili.util.WebChecker;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainFragment extends Fragment {
    private EditText searchText;
    private ImageButton searchBtn,menuBtn,searchConstraintBtn;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.mainlayout,container,false);
        searchText=view.findViewById(R.id.searchText);
        searchBtn=view.findViewById(R.id.searchButton);
        searchConstraintBtn=view.findViewById(R.id.searchConstraint);
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(WebChecker.isConnectIsNormal(getContext()))
                {
                    Intent intent=new Intent(getContext(),SearchActivity.class);
//                    Log.e("check",searchText.getText().toString());
                    intent.putExtra("keyWord",searchText.getText().toString());
                    getContext().startActivity(intent);
                }
                else
                {
                    Intent intent=new Intent(getContext(),NoWebEmptyActivity.class);
                    getContext().startActivity(intent);
                }
            }
        });
        searchConstraintBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),SearchConstraintActivity.class);
                getContext().startActivity(intent);
            }
        });
        menuBtn=view.findViewById(R.id.menuButton);
        menuBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),GridActivity.class);
                getContext().startActivity(intent);
            }
        });
        TabLayout tl = view.findViewById(R.id.tabLayout);
        ViewPager2 viewPager2 = view.findViewById(R.id.viewPager);
        viewPager2.setOffscreenPageLimit(10);
        viewPager2.setAdapter(new FragmentAdapter(getActivity()));
//        boolean[] tabList=FragmentAdapter.tabBoolList;
//        String[] tabNameList=FragmentAdapter.tabTitle;
        TabLayoutMediator tab = new TabLayoutMediator(tl, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//                String tabName=tabNameList[FragmentAdapter.getRealPosition(position)];
                String tabName=GridActivity.mUserList.get(position);
                tab.setText(tabName);
            }
        });
        tab.attach();
        return view;
    }
}
